//
//  playPage.m
//  HiddenTouch
//
//  Created by 한솔 이 on 12. 8. 28..
//  Copyright 2012년 __MyCompanyName__. All rights reserved.
//

#import "playPage.h"

#define BoxSize_w 40
#define BoxSize_h 40
#define HintSize_w 80
#define HintSize_h 80

#define Hidden01_01_left 160-BoxSize_w/2, 200-BoxSize_h/2
#define Hidden01_01_right 400-BoxSize_w/2, 200-BoxSize_h/2
#define Hidden01_01_left_ccp_w 160
#define Hidden01_01_left_ccp_h 200
#define Hidden01_01_right_ccp_w 400
#define Hidden01_01_right_ccp_h 200

#define Hidden02_01_left 120-BoxSize_w/2, 80-BoxSize_h/2
#define Hidden02_01_right 360-BoxSize_w/2, 80-BoxSize_h/2
#define Hidden02_01_left_ccp_w 120
#define Hidden02_01_left_ccp_h 80
#define Hidden02_01_right_ccp_w 360
#define Hidden02_01_right_ccp_h 80

#define Hidden03_01_left 40-BoxSize_w/2, 280-BoxSize_h/2
#define Hidden03_01_right 280-BoxSize_w/2, 280-BoxSize_h/2
#define Hidden03_01_left_ccp_w 40
#define Hidden03_01_left_ccp_h 280
#define Hidden03_01_right_ccp_w 280
#define Hidden03_01_right_ccp_h 280

#define hint_left 120-HintSize_w/2, 20-BoxSize_h/2
#define hint_left_ccp_w 120
#define hint_right_ccp_h 20

#define time_left 200-HintSize_w/2, 20-BoxSize_h/2
#define time_left_ccp_w 200
#define time_right_ccp_h 20

#define MAX_TIME 100


@implementation playPage


@synthesize bar, remaingTime, timeStatus;
//@synthesize myprogress;

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	playPage *layer = [playPage node];
	
	// add layer as a child to scene
	[scene addChild: layer];
// return the scene
	return scene;
}


// on "init" you need to initialize your instance
-(id) init
{
	if( (self=[super init])) {
        
        self.isTouchEnabled = YES;
        
        CCSprite *play001 = [CCSprite spriteWithFile:@"test.jpg"];
        play001.position = ccp(240, 160);
        [self addChild:play001];
        
        CCSprite *bottomback = [CCSprite spriteWithFile:@"bottomb.tiff"];
        bottomback.position = ccp(0, 0);
        bottomback.anchorPoint = ccp(0.0, 0.0);
        [self addChild:bottomback];
        
//        CCSprite *timeb = [CCSprite spriteWithFile:@"timeb.tiff"];
//        [timeb setTextureRect:CGRectMake(0, 0, 240, 40)];
//        timeb.position = ccp(240, 40);
//        timeb.anchorPoint = ccp(0.0, 1.0);
//        [self addChild:timeb];
//        
////        CCSprite *item = [CCSprite spriteWithFile:@"item.png"];
////        item.position = ccp(120, 40);
////        [self addChild:item];
//        
// 
//        timer = [CCProgressTimer progressWithFile:@"time.tiff"];
//        timer.type = kCCProgressTimerTypeHorizontalBarLR;
//        
//        timer.percentage = 60;
//        
//        [self addChild:timer];
//        timer.anchorPoint=ccp(0.0, 1.0);
//        [timer setPosition:ccp(241,38)];
//        
//        [self scheduleUpdate];
        remaingTime = MAX_TIME;
        
        bar = [CCSprite spriteWithFile:@"time.tiff"];
        [bar setTextureRect:CGRectMake(0, 0, 1, 0.1)];
        [bar setColor:ccBLACK];
        [self addChild:bar];
        [bar setPosition:ccp(240, 40)];
        [bar setScale:remaingTime];
        [bar setAnchorPoint:ccp(0.0, 1.0)];

        [self schedule:@selector(drainTime) interval:0.6];
    }
	return self;
}

-(void)drainTime
{
        if(remaingTime>0)
            remaingTime--;
    
    [bar setScaleX:remaingTime];
    
    
    
    int currentTimeStatus = self.timeStatus;
    
    if(remaingTime < 30)
        self.timeStatus = 1;
    else {
        self.timeStatus = 0;
    }
    
    if (currentTimeStatus ==0 && timeStatus ==1) {
        [bar runAction:[CCTintTo actionWithDuration:1 red:225 green:0 blue:0]];
    }
    
    else if (currentTimeStatus == 1 && timeStatus ==0) {
        [bar runAction:[CCTintTo actionWithDuration:1 red:0 green:255 blue:255]];
    }
    
    if (remaingTime<=0) {
        remaingTime=0;
        [[CCDirector sharedDirector]replaceScene:[CCTransitionFade transitionWithDuration:2 scene:[Gameover node] withColor:ccRED]];
    }
}



-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    CGPoint testTouch = [[CCDirector sharedDirector] convertToGL:location];
    
    
    if (CGRectContainsPoint(CGRectMake(Hidden01_01_left, BoxSize_w, BoxSize_h), testTouch) || CGRectContainsPoint(CGRectMake(Hidden01_01_right, BoxSize_w, BoxSize_h), testTouch)){

            a=1;        
        j=a+b+c;
        
        NSLog(@"탭 개수 %d", j);
         
        for (int i =0; i <= 1; i++) {
            CCSprite *ok_i = [CCSprite spriteWithFile:@"ok.png"];
            [ok_i setTextureRect:CGRectMake(0, 0, 37, 35)];
            ok_i.opacity = 0.0f;
            if (i==0) {
                ok_i.position = ccp(Hidden01_01_right_ccp_w, Hidden01_01_right_ccp_h);
                [ok_i runAction:[CCFadeIn actionWithDuration:0.5]];
            }else if (i==1){            
                ok_i.position = ccp(Hidden01_01_left_ccp_w, Hidden01_01_left_ccp_h);
                [ok_i runAction:[CCFadeIn actionWithDuration:0.5]];
            }
            
            if (j>=3) {
                CCScene *goclear = [CCTransitionFade transitionWithDuration:2 scene:[Gameclear scene]];
                [[CCDirector sharedDirector] replaceScene:goclear];
            }
            [self addChild:ok_i];
        }
    
    } else if (CGRectContainsPoint(CGRectMake(Hidden02_01_left, BoxSize_w, BoxSize_h), testTouch) || CGRectContainsPoint(CGRectMake(Hidden02_01_right, BoxSize_w, BoxSize_h), testTouch)){
            b=1; 
        j=a+b+c;
        NSLog(@"탭 개수 %d", j);

        for (int i =0; i <= 1; i++) {
            CCSprite *ok_i = [CCSprite spriteWithFile:@"ok.png"];
            [ok_i setTextureRect:CGRectMake(0, 0, 37, 35)];
            ok_i.opacity = 0.0f;
            if (i==0) {
                ok_i.position = ccp(Hidden02_01_right_ccp_w, Hidden02_01_right_ccp_h);
                [ok_i runAction:[CCFadeIn actionWithDuration:0.5]];
            }else if (i==1){            
                ok_i.position = ccp(Hidden02_01_left_ccp_w, Hidden02_01_left_ccp_h);
                [ok_i runAction:[CCFadeIn actionWithDuration:0.5]];
            }

            if (j>=3) {
                CCScene *goclear = [CCTransitionFade transitionWithDuration:2 scene:[Gameclear scene]];
                [[CCDirector sharedDirector] replaceScene:goclear];
            }
            [self addChild:ok_i];
        }
        
        
    } else if (CGRectContainsPoint(CGRectMake(Hidden03_01_left, BoxSize_w, BoxSize_h), testTouch) || CGRectContainsPoint(CGRectMake(Hidden03_01_right, BoxSize_w, BoxSize_h), testTouch)){
            c=1; 
        j=a+b+c;
        NSLog(@"탭 개수 %d", j);
        
        for (int i =0; i <= 1; i++) {
            CCSprite *ok_i = [CCSprite spriteWithFile:@"ok.png"];
            [ok_i setTextureRect:CGRectMake(0, 0, 37, 35)];
            ok_i.opacity = 0.0f;
            if (i==0) {
                ok_i.position = ccp(Hidden03_01_right_ccp_w, Hidden03_01_right_ccp_h);
                [ok_i runAction:[CCFadeIn actionWithDuration:0.5]];
                
            }else if (i==1){            
                ok_i.position = ccp(Hidden03_01_left_ccp_w, Hidden03_01_left_ccp_h);
                [ok_i runAction:[CCFadeIn actionWithDuration:0.5]];
            }

            if (j>=3) {
                CCScene *goclear = [CCTransitionFade transitionWithDuration:2 scene:[Gameclear scene]];
                [[CCDirector sharedDirector] replaceScene:goclear];
            }
            [self addChild:ok_i];
        }
    }
    else{
        CCSprite *no_n = [CCSprite spriteWithFile:@"no.png"];
        [no_n setTextureRect:CGRectMake(0, 0, 35, 31)];
        [no_n runAction:[CCSequence actions:[CCFadeIn actionWithDuration:0.2], [CCFadeOut actionWithDuration:0.2],nil]];
        no_n.position=ccp(testTouch.x, testTouch.y);
        if (no_n.position.y>=40)
        {
        remaingTime -= 10;
            if (remaingTime<10) {
                remaingTime -=remaingTime;
            }
        }
        else 
        {
            no_n.position=ccp(24255,23953);
            
                
            if (CGRectContainsPoint(CGRectMake(hint_left, HintSize_w, BoxSize_h), testTouch)) {
                if (a==0) {
                    CCSprite *ok = [CCSprite spriteWithFile:@"ok.png"];
                    [ok setTextureRect:CGRectMake(0, 0, 37, 35)];
                                ok.opacity = 0.0f;
                    [ok runAction:[CCSequence actions:[CCFadeIn actionWithDuration:1.5], [CCFadeOut actionWithDuration:1.5],nil]];
                    ok.position = ccp(Hidden01_01_right_ccp_w, Hidden01_01_right_ccp_h);
                    k=k+1;
                    NSLog(@"%d",k);
                    if (k>3) {
                        ok.position = ccp(23434,23425);
                    }
                    [self addChild:ok];
                }
                else if (b==0) {
                    CCSprite *ok = [CCSprite spriteWithFile:@"ok.png"];
                    [ok setTextureRect:CGRectMake(0, 0, 37, 35)];
                    ok.opacity = 0.0f;
                    [ok runAction:[CCSequence actions:[CCFadeIn actionWithDuration:1.5], [CCFadeOut actionWithDuration:1.5],nil]];
                    ok.position = ccp(Hidden02_01_right_ccp_w, Hidden02_01_right_ccp_h);
                    k=k+1;
                    if (k>3) {
                        ok.position = ccp(23434,23425);
                    }
                    [self addChild:ok];
                }
                else if (c==0) {
                    CCSprite *ok = [CCSprite spriteWithFile:@"ok.png"];
                    [ok setTextureRect:CGRectMake(0, 0, 37, 35)];
                    ok.opacity = 0.0f;
                    [ok runAction:[CCSequence actions:[CCFadeIn actionWithDuration:1.5], [CCFadeOut actionWithDuration:1.5],nil]];
                    ok.position = ccp(Hidden03_01_right_ccp_w, Hidden03_01_right_ccp_h);
                    k=k+1;
                    if (k>3) {
                        ok.position = ccp(23434,23425);
                    }
                    [self addChild:ok];
                }
                }
        }

        [self addChild:no_n];
        
        if (CGRectContainsPoint(CGRectMake(time_left, HintSize_w, BoxSize_h), testTouch)){
            l=l+1;
            if (l<4) {
                remaingTime = 100;
        [bar runAction:[CCTintTo actionWithDuration:1 red:0 green:255 blue:255]];
            }
            else {
                remaingTime=remaingTime;
            }
        }
    }
}

//-(void)update:(ccTime)dt
//{
//    timer.percentage -= dt *1;
//}      

@end
